<?php

namespace beObject\Web\Mail;

use beObject\Primitive\Boolean;
use beObject\Primitive\String;

  class SmtpMail {

    /**
     * Set the name of the SMTP relay mail server to use to send an email.
     * @param String $value The name of the SMTP mail server.
     * @return String Return true on succes or false on failure.
     * @link http://be.php.net/manual/en/function.is-string.php
     * @link http://be.php.net/manual/en/function.empty.php
     * @link http://be.php.net/manual/en/function.ini-set.php<br />
     * @throws BeObjectRuntimeException
     */
    public static function server(String $value = null) {
      $return = String::EMPTY_STRING;

      if ($value == null) {
        $return = new String(ini_get("SMTP"));
      } else {
        $return = ini_set("SMTP", $value->toString());
      }

      if ($return === false) {
        throw new BeObjectRuntimeException("The SMTP property can not be modified.");
      } else {
        $return = new String($return);
      }
      return $return;
    }

    /**
     * Send a mail message using the specified destination parameters.
     * @param MailMessage $mailMessage The MailMessage to send.
     * @return \beObject\Primitive\Boolean Returns true on succes or false on failure.
     * @link http://be.php.net/manual/en/function.empty.php
     * @link http://be.php.net/manual/en/function.mail.php
     */
    public static function send(MailMessage $mailMessage) {
      $headers = "";
      $body = "";

      if (count($mailMessage->attachments) > 0) {
        $headers .= "MIME-Version: 1.0\r\n" .
                "Content-Type: multipart/mixed; boundary=\"==Multipart_Boundary_x" . $mailMessage->attachments[0]->getHash() . "x\"";
      } elseif ($mailMessage->isHtml()->booleanValue() === true) {
        $headers .= "MIME-Version: 1.0\r\n" .
                "Content-type: text/html; charset=iso-8859-1";
      } else {
        $headers .= "MIME-Version: 1.0\r\n" .
                "Content-type: text/plain; charset=iso-8859-1";
      }

      if ($mailMessage->getSenderName() != "" && $mailMessage->getSender() != "") {
        $headers .= "\r\nFrom: \"" . $mailMessage->getSenderName()->toString() . "\" <" . $mailMessage->getSender()->toString() . ">";
      } elseif ($mailMessage->getSender() != "") {
        $headers .= "\r\nFrom: <" . $mailMessage->getSender()->toString() . ">";
      }

      if ($mailMessage->getReply() != "") {
        $headers .= "\r\nReply-To: <" . $mailMessage->getReply()->toString() . ">";
      }

      if ($mailMessage->getCopy() != "") {
        $headers .= "\r\nCC: <" . $mailMessage->getCopy()->toString() . ">";
      }

      if ($mailMessage->getHiddenCopy() != "") {
        $headers .= "\r\nBCC: <" . $mailMessage->getHiddenCopy()->toString() . ">";
      }

      if (count($mailMessage->attachments) > 0) {
        $attachment = String::EMPTY_STRING;

        for ($i = 0; $i < count($mailMessage->attachments); ++$i) {
          $attachment .= "\n\n" . "--==Multipart_Boundary_x" . $mailMessage->attachments[$i]->getHash() . "x\n" .
                  "Content-Type: application/octet-stream; name=\"" . $mailMessage->attachments[$i]->getFilename() . "\"\r\n" .
                  "Content-Description: " . $mailMessage->attachments[$i]->getFilename() . "\r\n" .
                  "Content-Transfer-Encoding: base64\r\n" .
                  "Content-Disposition: attachment; " .
                  "filename=\"" . $mailMessage->attachments[$i]->getFilename() . "\"\r\n\n"
                  . $mailMessage->attachments[$i]->getFileContent();
        }

        $body = "--==Multipart_Boundary_x" . $mailMessage->attachments[0]->getHash() . "x\n" .
                "Content-Type: text/plain; charset=iso-8859-1\r\n" .
                "Content-Transfer-Encoding: 7bit\r\n\n" .
                $mailMessage->getBody()->toString() . $attachment . "\r\n\r\n" . "--==Multipart_Boundary_x" . $mailMessage->attachments[0]->getHash() . "x--";
      } else {
        $body = $mailMessage->getBody()->toString();
      }

      return new Boolean(mail($mailMessage->getRecipient()->toString(), $mailMessage->getSubject()->toString(), $body, $headers));
    }

  }

?>
